package com.scrcu.core.batch.bean;

import com.boarsoft.common.util.InetUtil;
import com.scrcu.core.batch.entity.BatchExecution;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 批量应用信息
 */
@Setter
@Getter
public class BatchAppStat implements Serializable {

    private static final long serialVersionUID = -6331986369674101949L;

    /**
     * 批量实例 IP:port
     */
    private String addr;

    /**
     * 所属分组（业务群），对应服务的分组
     */
    private String catalog;

    /**
     * 状态
     */
    private short status;

    /**
     * 批量的执行信息集
     */
    private Map<String, List<BatchExecution>> instMap = new HashMap<>();

    /**
     * 构造方法
     *
     * @param catalog
     */
    public BatchAppStat(String catalog) {
        this.catalog = catalog;
        this.addr = InetUtil.getAddr();
    }

    /**
     * 构造方法
     *
     * @param catalog
     * @param addr
     * @param status
     */
    public BatchAppStat(String catalog, String addr, short status) {
        this.catalog = catalog;
        this.addr = addr;
        this.status = status;
    }

    /**
     * 添加批量任务执行信息
     *
     * @param execution
     */
    public void addInst(BatchExecution execution) {
        String k = execution.getCode();
        List<BatchExecution> lt = instMap.get(k);
        if (lt == null) {
            lt = new ArrayList<BatchExecution>();
            instMap.put(k, lt);
        }
        lt.add(execution);
    }

}
